/**
 * 模拟FormData类
 */
export default class TestFormData {
    private _boundary_key: string;
    private _boundary: string;
    private _end_boundary: string;
    private _result: string;

    constructor() {
        this._boundary_key = "testformdata";
        this._boundary = "";
        this._end_boundary = "";
        this._result = "";

        this._boundary = "--" + this._boundary_key;
        this._end_boundary = this._boundary + "--";
        this._result = "";
    }
    append(key, value) {
        this._result += this._boundary + "\r\n";
        this._result += 'Content-Disposition: form-data; name="' + key + '"' + "\r\n\r\n";
        this._result += encodeURIComponent(value) + "\r\n";
    }

    appendImageFile(key, value, fileName) {
        this._result += this._boundary + "\r\n";
        this._result += `Content-Disposition: form-data; name="${key}"; filename="${fileName}"\r\n` + "Content-Type: image/png\r\n\r\n"; // todo ..application/octet-stream
        //this._result += String.fromCharCode.apply(null, value) + "\r\n";
        var chunk = 8 * 1024;
        var i;
        for (i = 0; i < value.length / chunk; i++) {
            this._result += String.fromCharCode.apply(null, value.slice(i * chunk, (i + 1) * chunk));
        }
        this._result += String.fromCharCode.apply(null, value.slice(i * chunk)) + "\r\n";
    }

    appendVideoFile(key, value, fileName) {
        this._result += this._boundary + "\r\n";
        this._result += `Content-Disposition: form-data; name="${key}"; filename="${fileName}"\r\n` + "Content-Type: video/mpeg4\r\n\r\n"; // todo ..application/octet-stream
        //this._result += String.fromCharCode.apply(null, value) + "\r\n";
        var chunk = 8 * 1024;
        var i;
        for (i = 0; i < value.length / chunk; i++) {
            this._result += String.fromCharCode.apply(null, value.slice(i * chunk, (i + 1) * chunk));
        }
        this._result += String.fromCharCode.apply(null, value.slice(i * chunk)) + "\r\n";
    }

    appendMusicFile(key, value, fileName) {
        this._result += this._boundary + "\r\n";
        this._result += `Content-Disposition: form-data; name="${key}"; filename="${fileName}"\r\n` + "Content-Type: audio/mp3\r\n\r\n"; // todo ..application/octet-stream
        //this._result += String.fromCharCode.apply(null, value) + "\r\n";
        var chunk = 8 * 1024;
        var i;
        for (i = 0; i < value.length / chunk; i++) {
            this._result += String.fromCharCode.apply(null, value.slice(i * chunk, (i + 1) * chunk));
        }
        this._result += String.fromCharCode.apply(null, value.slice(i * chunk)) + "\r\n";
    }

    arrayBuffer() {
        this._result += this._end_boundary;
        let charArr = [];

        for (var i = 0; i < this._result.length; i++) {
            // 取出文本的charCode（10进制）
            charArr.push(this._result.charCodeAt(i));
        }

        let array = new Uint8Array(charArr);
        return array.buffer;
    }
}

//这是一个简单的板栗
// var formDatas = require("M_fromData")
// var formData = new formDatas();
// formData.appendFile("file", fileUtils.read, "fileName.png");

// var xhr = new XMLHttpRequest();
// xhr.timeout = 3000;
// xhr.open('POST', 'http://xxxxxx', true);

// //记得在头部带上boundary，这里注意一定要和类里面的 _boundary_key 保持一致
// xhr.setRequestHeader('Content-Type', 'multipart/form-data; boundary=testformdata');

// xhr.onload = function (e) {
//     if (xhr.readyState === 4 && (xhr.status >= 200 && xhr.status < 300)) {
//         console.log("xhr.responseText::", xhr.responseText)
//     }
// };
// // ArrayBuffer
// xhr.send(formData.arrayBuffer());